package com.lvmama.controller.nps;

import com.github.pagehelper.PageInfo;
import com.lvmama.common.enums.NpsQuestionTypeEnum;
import com.lvmama.common.result.Result;
import com.lvmama.common.utils.DateUtil;
import com.lvmama.pojo.dto.nps.NPSCommitDTO;
import com.lvmama.pojo.entity.nps.NPSCommit;
import com.lvmama.pojo.entity.nps.NPSStatistics;
import com.lvmama.service.nps.NpsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/nps")
@Slf4j
public class NpsCommitController {

    @Autowired
    private NpsService npsService;


    /**
     * 查询nps分页
     *
     * @param npsCommitDTO 查询条件
     * @return 分页
     */
    @PostMapping(value = "/queryNpsCommitList")
    public Result<PageInfo<NPSCommit>>  queryNpsCommitList(@RequestBody NPSCommitDTO npsCommitDTO){
        PageInfo<NPSCommit> pageInfo = null;
        log.info("get list start. query:{}" + npsCommitDTO);
        try {
            if (NpsQuestionTypeEnum.INTERNAL.getCode().equals(npsCommitDTO.getNpsQuestionType())) {
                pageInfo = npsService.queryNPSInternalCommitPage(npsCommitDTO).getData();
            } else {
                pageInfo = npsService.selectNpsCommitList(npsCommitDTO).getData();
            }
        } catch (Exception e) {
            log.error("get list error:" + e.getMessage(), e);
        }
        log.info("get list end.");
        return Result.success(pageInfo);

    }

    /**
     * 查询nps统计数据
     *
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/npsStatistics")
    public Result<NPSStatistics> npsStatistics(@RequestBody NPSCommitDTO query) {
        NPSStatistics statistics = null;
        log.info("get npsStatistics start. query:{}" + query);
        try {
            if (NpsQuestionTypeEnum.INTERNAL.getCode().equals(query.getNpsQuestionType())) {
                statistics = npsService.queryNpsInternalStatistics(query).getData();
            } else {
                statistics = npsService.queryNpsStatistics(query).getData().get(0);
            }
        } catch (Exception e) {
            log.error("get npsStatistics error:" + e.getMessage(), e);
        }
        log.info("get npsStatistics end.");
        return Result.success(statistics);
    }


    /**
     * 导出问卷查询内容到excel
     *
     * @param query 查询条件
     */
    @PostMapping("/exportQuestionnaire")
    public Result<Map<String,List<Object[]>>>  exportQuestionnaire(@RequestBody NPSCommitDTO query) {
        List<NPSCommit> commits;
        Map<String, List<Object[]>> resultMap = new HashMap<>();
        log.info("exportQuestionnaire start. query:" + query);
        try {
            List<Object[]> dataList = new ArrayList<>();
            if (NpsQuestionTypeEnum.INTERNAL.getCode().equals(query.getNpsQuestionType())) {
                commits = npsService.queryNPSInternalCommitList(query).getData();
                for (NPSCommit npsCommit:commits) {
                    String[] str = {npsCommit.getId(),npsCommit.getCreateTime().toString(),npsCommit.getScore().toString(),npsCommit.getRemark()};
                    dataList.add(str);
                }
            } else {
                commits = npsService.queryNPSCommitList(query).getData();
                for (NPSCommit npsCommit:commits) {
                    String[] str = {npsCommit.getId(), DateUtil.dateFormat(npsCommit.getCreateTime(), DateUtil.PATTERN_YYYY_MM_DD_HHMMSS) ,npsCommit.getUserId(),
                            npsCommit.getPhoneNumber(),npsCommit.getCategoryName(), npsCommit.getOrderId(),
                            npsCommit.getCommitType(),npsCommit.getScore().toString(),npsCommit.getRemark()};
                    dataList.add(str);
                }
            }
            resultMap.put("dataList",dataList);
        } catch (Exception e) {
            log.error("exportQuestionnaire error:" + e.getMessage(), e);
        }
        log.info("exportQuestionnaire end.");
        return Result.success(resultMap);
    }



}
